package com.globallogic.week1xml.util;

import generated.Authors;
import generated.Book;
import generated.Books;
import generated.Catalog;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Util {
	public static Catalog getCatalog(){
		Book book1 = new Book();
		book1.setId(new BigInteger("111"));
		book1.setName("Learning JAXB");
		book1.setISBN(new BigInteger("123445"));
		book1.setPrice("34 $");
		
		Authors janeDoe = new Authors();
		janeDoe.getName().add("Jane Doe");
		book1.setAuthors(janeDoe);
		book1.setDescription("Step by step instructions for beginners");
		book1.setPublishDate("2003-01-01");
		
		Book book2 = new Book();
		book2.setId(new BigInteger("222"));
		book2.setName("Java Webservices today and Beyond");
		book2.setISBN(new BigInteger("522965"));
		book2.setPrice("29 $");
		
		Authors brownPeter = new Authors();
		brownPeter.getName().add("John Brown");
		brownPeter.getName().add("Peter T.");
		book2.setAuthors(brownPeter);
		book2.setDescription("Information for users so that they can start"
				+ " using Java Web Services Developer Pack");
		book2.setPublishDate("2002-11-01");
		
		Books books = new Books();
		books.getBook().add(book1);
		books.getBook().add(book2);
		
		Catalog catalog = new Catalog();
		catalog.setBooks(books);
		
		return catalog;
	}
	
	public static Catalog getWrongCatalog(){
		Book book1 = new Book();
		book1.setId(new BigInteger("111"));
		book1.setName("Learning JAXB");
		book1.setISBN(new BigInteger("123445444"));
		book1.setPrice("34 $");
		
		Authors janeDoe = new Authors();
		janeDoe.getName().add("Jane Doe");
		book1.setAuthors(janeDoe);
		book1.setDescription("Step by step instructions for beginners");
		book1.setPublishDate("2003-01-01");
		
		Book book2 = new Book();
		book2.setId(new BigInteger("222"));
		book2.setName("Java Webservices today and Beyond");
		book2.setISBN(new BigInteger("522965"));
		book2.setPrice("29 $");
		
		Authors brownPeter = new Authors();
		brownPeter.getName().add("John Brown");
		brownPeter.getName().add("Peter T.");
		book2.setAuthors(brownPeter);
		book2.setDescription("Information for users so that they can start"
				+ " using Java Web Services Developer Pack");
		book2.setPublishDate("2002-11-01");
		
		Books books = new Books();
		books.getBook().add(book1);
		books.getBook().add(book2);
		
		Catalog catalog = new Catalog();
		catalog.setBooks(books);
		
		return catalog;
	}
	
	public static String getResourse(String resourseName){
		StringBuilder xmlString = new StringBuilder();
		
		try {
			BufferedReader br = new BufferedReader(
					new InputStreamReader(
							Catalog.class.getClassLoader().getResourceAsStream("\\resourses\\"+resourseName)));
			String line;
			
			while((line = br.readLine()) != null){
				xmlString.append(line+"\n");
			}
			
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return xmlString.toString();
	}
	
	public static InputStream load(){
		InputStream is = null;
		is = Catalog.class.getClassLoader().getResourceAsStream("\\resourses\\books.xml");
		return is;
	}
	
	public static File getFile(String pathString){
		Path path = Paths.get(pathString);
		
		if (!Files.exists(path) || Files.isDirectory(path)){
			return null;
		} 
		
		return new File(pathString);
	}
}
